package asc.common.util;

//base class
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;

import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;

public class StringUtil {
	

	public static String checkNull(String str) {
		return (str==null||str.equals(""))? "":str;
	}

	public static String checkNull(String str, String replaceStr) {
		return (str==null||str.equals(""))? replaceStr:str;
	}

	public static String toUpperCase(String str) {
		return (str==null)? "":str.toUpperCase();
	}

	public static String toLowerCase(String str) {
		return (str==null)? "":str.toLowerCase();
	}

	public static String toKR(String str) {
		if (str == null || str.trim().equals("")) return "";
		try {
			return new String(str.getBytes("ISO-8859-1"), "EUC-KR"); //EUC-KR
		}catch (UnsupportedEncodingException ex) {
			return null;
		}
	}

	public static String toKrMs(String str) {
		if (str == null || str.trim().equals("")) return "";
		try {
			return new String(str.getBytes("ISO-8859-1"), "MS949");
		} catch (UnsupportedEncodingException ex) {
			return null;
		}
	}

	public static String toAscii(String str) {
		if (str == null || str.trim().equals("")) return "";
		try {
			return new String(str.getBytes("EUC-KR"), "ISO-8859-1");
		} catch (UnsupportedEncodingException ex) {
			return null;
		}
	}

	public static String toURLEncodeUtf8(String str) {
		if (str == null || str.trim().equals("")) return "";
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException ex) {
			return null;
		}
	}

	public static String replace(String original, String find, String replace) {
		if(original==null || find==null || replace==null || original.length()<1 || find.length()<1) return original; 
		int index=-1, fromIndex=0, tempIndex;
		StringBuffer sb=new StringBuffer();
		while((tempIndex=original.indexOf(find, fromIndex))>=0)
		{
			index=tempIndex;
			sb.append(original.substring(fromIndex, index)).append(replace);
			fromIndex=index+find.length();
		}
		if(sb.length()<1) return original;
	
		sb.append(original.substring(index+find.length()));
		return sb.toString();
	}


	public static String shortString(String s, int max){

		String result = "";
		int count = 0;
	
		if(s == null)
			return result;
	
		if(s.getBytes().length > max) {
	
			max -= 2;
			char buf[] = s.toCharArray();
		
			for(int i=0; i < max && i < s.length(); i++) {
		
				if(buf[i] >= 0xa100 && buf[i] <= 0xfe00) {
					count += 2;
		
				} else {
					count ++;
				}
		
				if(count > max) {
					result += "...";
					break;
		
				} else {
					result += (new String(buf, i, 1));
				}
	
			}
		} else {
			result = s;
		}
	
		return result;
	}

	public static String leftPadding(String src, String ch, int num) {
		String result = "";

		if(src == null || src.length() >= num) {
			return src;
		}

		int cnt = num - src.length();

		for(int i=0; i < cnt; i++) {
			result += ch;
		}

		return result+src;
	}

	public static String RightPadding(String src, String ch, int num) {
		String result = "";

		if(src == null || src.length() >= num) {
			return src;
		}
	
		int cnt = num - src.length();
	
		for(int i=0; i < cnt; i++) {
			result += ch;
		}
	
		return src+result;
	}

	public static String trim(String str) {
		return trim(str, "");
	}
	public static String trim(String str, String defValue) {
		if (str == null || "".equals(str)) {
			return defValue;
		} else if (str.trim().length() == 0) {
			return defValue;
		} else {
			return str.trim();
		}
	}

	public static String trim(Object src, String defValue) {
		if (src != null) {
			return src.toString().trim();
		} else {
			return defValue;
		}
	}

	public static String getNumber(String string){
		if(string == null) return "";

		char[] source = string.toCharArray();
		char[] result = new char[source.length];
		int j = 0;
		for (int i = 0 , y = result.length; i < y ; i++ ){
			if (Character.isDigit(source[i])) result[j++] = source[i];
		}
		return new String(result, 0 , j);
	}

	public static int getStringLength(String str)
	{
		char ch[] = str.toCharArray();
		int max = ch.length;
		int count = 0;
	
		for (int i = 0; i < max; i++)
		{
			if (ch[i] > 0x80)
			count++;
			count++;
		}
		return count;
	}

	public static String fillZero( int length, long lvalue )
	{
		String value = "" +lvalue;
		return fillZero( length, value );
	}
	
	public static String fillZero( int length, String value )
	{
		if (value == null ) return "";
	
		char[] cValue = value.toCharArray();
		for (int i = 0; i < cValue.length; i++)
		{
			if( !Character.isDigit( cValue[i] ) ){
				return "";
			}
		}
	
		String result = value;
		int intLength = getStringLength(result);
	
		if(intLength == length)
			return result;
		else if (intLength > length)
			return hanSubstr( length, value );
	
		for (int i = 0; i < length; i++)
		{
			result = "0" + result;
			i = getStringLength(result)-1;
		}
		return result;
	}
	
	public static String hanSubstr(int length, String value) {
		if (value == null || value.length() == 0)
			return "";

		int szBytes = value.getBytes().length;

		if (szBytes <= length)
			return value;

		String result = new String(value.getBytes(), 0, length);
		if( result.equals("") )
			result = new String(value.getBytes(), 0, length-1);
		
		return result;
	}

	public static String getFormat(String str, String format) {
		if( format == null || format.equals("") ) format = "###,###,###.###";
	
		String temp = null;
	
		if (str == null)
			temp = "0";
		else {
			double change = Double.valueOf(str).doubleValue();
			DecimalFormat decimal = new DecimalFormat(format);
			temp = decimal.format(change);	
		}

		return temp;
	}

	public static String getFormat(int istr, String format) {
		String str = Integer.toString(istr);
		if( format == null || format.equals("") ) format = "###,###,###.###";
	
		String temp = null;
	
		if (str == null)
			temp = "0";
		else {
			double change = Double.valueOf(str).doubleValue();
			DecimalFormat decimal = new DecimalFormat(format);
			temp = decimal.format(change);	
		}
	
		return temp;
	}
	
	public static String replaceByPattern(String text, String pattern, String newStr) {
		return replaceByPattern(text, pattern, newStr, true);
	}

	public static String replaceByPattern(String text, String pattern, String newStr, boolean ignoreCase) {
		try {
			String retString = text;
			RE reg = null;
		
			reg = (ignoreCase) ? new RE(pattern, RE.MATCH_CASEINDEPENDENT) : new RE(pattern);
		
			if (reg.match(text)) {
				retString = reg.subst(text, newStr);
			}
			return retString;
			
		} catch (RESyntaxException e) { 
			e.printStackTrace();
			return text;
		}		
	}
	
	public static String ZipCode(String ZipCode){
		String strnew = "";
		if (ZipCode == null) return "";
		if (ZipCode.length() == 6) strnew = ZipCode.substring(0, 3) + "-" + ZipCode.substring(3, 6);
		if (ZipCode.length() == 7) strnew = ZipCode.substring(0, 3) + ZipCode.substring(4, 7);
		return strnew;
	}

	public static String JuminNo(String JuminNo){
		if (JuminNo == null) return "";
		if( JuminNo.length() <= 0 || ( JuminNo.length() != 13 && JuminNo.length() != 14 ) ) return JuminNo;
		String strRet = "";
		if( JuminNo.length() == 13 ){
			strRet = JuminNo.substring(0, 6) + "-" + JuminNo.substring(6);
		}else if( (JuminNo.length() == 14) ){
			strRet = JuminNo;
		}
		return strRet;
	}

	public static String getSexName(String juminNo, String gubun){
		if (juminNo == null) return "";
		if (juminNo.length() == 0) return "";
		if (juminNo.length() < 13) return "";
		
		juminNo = StringUtil.replace(juminNo,"-","");
		
		String RearOfJuminNO = ( juminNo.length() == 13 ? juminNo.substring(6) : 
								 juminNo.length() == 14 ? juminNo.substring(7) : "");
		
		char ch = RearOfJuminNO.charAt(0);

		if ((ch == '1') || (ch == '3') || (ch == '5') || (ch == '7')) return "KO".equals(gubun)?"??":"M";
		if ((ch == '2') || (ch == '4') || (ch == '6') || (ch == '8')) return "KO".equals(gubun)?"??":"F";
		
		return "";
	}

	public static String getAge(String JuminNO){
		if (JuminNO == null) return "";
		if (JuminNO.length() == 0) return "";
		if (JuminNO.length() < 13) return "";
		
		JuminNO = StringUtil.replace(JuminNO,"-","");
		
		int nAge = 0;

		int gubun = CastUtil.toInt(JuminNO.substring(6, 7));
		String ssn1 = JuminNO.substring(0, 6);
		
		switch( gubun ){
			case 1:
			case 2:
			case 5:
			case 6:
				JuminNO = "19" + ssn1;
				break;
			case 3:
			case 4:
			case 7:
			case 8:
				JuminNO = "20" + ssn1;
				break;
			default:
				JuminNO = "-1";
				break;
		}
		if( JuminNO.equals("-1") ) return "";
		
		int nBirthYear		= CastUtil.toInt(JuminNO.substring(0, 4));
		int nBirthMonth	   = CastUtil.toInt(JuminNO.substring(4, 6));
		int nBirthDay		 = CastUtil.toInt(JuminNO.substring(6, 8));

		String strCurrentDate = DateUtil.getCurrentTime("yyyyMMdd");
		int nCurrentYear	  = Integer.parseInt(strCurrentDate.substring(0, 4));
		int nCurrentMonth	 = Integer.parseInt(strCurrentDate.substring(4, 6));
		int nCurrentDay	   = Integer.parseInt(strCurrentDate.substring(6, 8));
		int nMonth = 0;

		if (nCurrentYear > nBirthYear){
			nAge = nCurrentYear - 1 - nBirthYear;
		}   

		if (nCurrentMonth > nBirthMonth){
			nAge += 1;

			if (nCurrentDay > nBirthDay){
				nMonth = nCurrentMonth - nBirthMonth;
			}else{
				nMonth = nCurrentMonth - 1 - nBirthMonth;
			}   
		}else{
			if (nCurrentMonth == nBirthMonth){
				if (nCurrentDay > nBirthDay){
					nAge += 1;
				}else{
					if (nCurrentYear != nBirthYear){
						nMonth = 11;
					}   
				}
			}else{
				nMonth = (nCurrentMonth + 12) - nBirthMonth;

				if (nCurrentDay <= nBirthDay){
					nMonth -= 1;
				}
			}
		}
			
		return ""+nAge ;
	}

	public static String getBirthday(String JuminNO){
		if (JuminNO == null) return "";
		if (JuminNO.length() == 0) return "";
		if (JuminNO.length() < 13) return "";
		
		JuminNO = StringUtil.replace(JuminNO,"-","");
		
		String retVal = "";
		
		int gubun = CastUtil.toInt(JuminNO.substring(6, 7));
		String ssn1 = JuminNO.substring(0, 6);
		
		switch( gubun ){
			case 1:
			case 2:
			case 5:
			case 6:
				JuminNO = "19" + ssn1;
				break;
			case 3:
			case 4:
			case 7:
			case 8:
				JuminNO = "20" + ssn1;
				break;
			default:
				JuminNO = "-1";
				break;
		}
		if( JuminNO.equals("-1") ) return "";

		retVal = JuminNO;
		
		return retVal;
	}

}
